/**
 * JAVACC DEMO 1.0
 */
package com.apache.passport.aop;

import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.Validator;
import com.apache.passport.common.FileUtils;
import com.apache.passport.entity.LoginInfo;
import com.apache.passport.service.CacheHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * description:  定时器执行任务
 *
 * @author Hou Dayu 创建时间：2016-1-16
 */
public class LoginAopTimer {

    private static final Logger log = LoggerFactory.getLogger(LoginAopTimer.class);

    /**
     * description:  从缓存中取出所有对象,且保存到文件中,定时器会调用此方法
     */
    public void useAllCacheBackFile() {
        log.info("passport msg:[{}]", "定时器执行开始...");
        ParamsVo<LoginInfo> param = new ParamsVo<LoginInfo>();
        //去除当前缓存中所有的key
        List<String> lst = CacheHelper.getInstance().getCache("loginLogCache").getAllKeys();
        List<Object> listObj = CacheHelper.getInstance().getCache("loginLogCache").getObjects(lst);
        if (!Validator.isEmpty(listObj)) {
            for (int i = 0; i < listObj.size(); i++) {
                LoginInfo info = (LoginInfo) listObj.get(i);
                param.setObj(info);
                FileUtils.getInstance().createLoginFile(param);
            }
            CacheHelper.getInstance().getCache("loginLogCache").removeAll();
        }
    }
}
